<?php

namespace App\Models;

use Illuminate\Support\Facades\DB;

class UserFootprint extends Model
{
    protected $table = 'yf_user_footprint';
    protected $primaryKey = 'footprint_id';
    public $timestamps = false;
    protected $guarded = ['footprint_id'];

    public static function userFavorite(int $userId = 0) : array
    {
        if ($userId) {
            $common_ids = self::from('yf_user_footprint as a')
                ->select('a.common_id')
                ->leftJoin('yf_goods_common as b', 'b.common_id', '=', 'a.common_id')
                ->where([
                    ['b.common_state', '=', 1],
                    ['b.is_del', '=', 1],
                    ['b.common_verify', '=', 1],
                    ['b.shop_status', '=', 3],
                    ['b.common_verify', '=', 1],
                    ['a.user_id', '=', $userId],
                ])
                ->distinct()
                ->latest('a.footprint_time')
                ->limit(10)
                ->pluck('common_id')
                ->toArray();

            $goods_cat_ids = GoodsCommon::whereIn('common_id', $common_ids)->pluck('cat_id')->toArray();
        } else {
            $goods_cat_ids = GoodsRecommend::limit(100)->pluck('goods_cat_id')->toArray();
        }

        $recommendGoods = DB::table('yf_goods_base as a')
            ->select('a.*', 'b.is_immediate', 'c.district_id', 'c.shop_qq')
            ->leftJoin('yf_goods_common as b', 'b.common_id', '=', 'a.common_id')
            ->leftJoin('yf_shop_base as c', 'c.shop_id', '=', 'a.shop_id')
            ->where([
                ['a.goods_is_shelves', '=', 1],
                ['a.is_del', '=', 1],
                ['b.common_state', '=', 1],
                ['b.common_verify', '=', 1],
                ['b.is_del', '=', 1],
                ['c.shop_status', '=', 3],
                ['c.shop_type', '=', 1]
            ])
            ->when($goods_cat_ids, function ($query) use ($goods_cat_ids) {
                return $query->whereIn('a.cat_id', $goods_cat_ids);
            })
            ->distinct()
            ->inRandomOrder()
            ->limit(10)
            ->get();

        $size = $recommendGoods->count();
        $goods_ids = $recommendGoods->pluck('goods_id')->toArray();

        if ($size < 10) {
            $data = DB::table('yf_goods_base as a')
                ->select('a.*', 'c.district_id')
                ->leftJoin('yf_shop_base as c', 'c.shop_id', '=', 'a.shop_id')
                ->where([
                    ['a.goods_is_shelves', '=', 1],
                    ['a.is_del', '=', 1],
                    ['c.shop_status', '=', 3],
                    ['c.shop_type', '=', 1]
                ])
                ->when($goods_ids, function ($query) use ($goods_ids) {
                    return $query->whereNotIn('a.cat_id', $goods_ids);
                })
                ->distinct()
                ->inRandomOrder()
                ->limit(10-$size)
                ->get();

            $recommendGoods = $recommendGoods->merge($data);
        }

        $district_ids = $recommendGoods->pluck('district_id')->toArray();
        $common_ids = $recommendGoods->pluck('common_id')->toArray();

        $districts = BaseDistrict::whereIn('district_id', $district_ids)->get();
        // 店铺所在的二级地址
        $district_parents = BaseDistrict::whereIn('district_id', $districts->pluck('district_parent_id')->toArray())->get();
        // 折扣商品信息
        $discount_goods = DiscountGoods::where('discount_goods_state', 1)->whereIn('common_id', $common_ids)->get();
        $recommendGoods->each(function ($recommendGood) use ($districts, $district_parents, $discount_goods) {
            $district = $districts->where('district_id', $recommendGood->district_id)->first();
            $district_parent = $district_parents->where('district_id', $district->district_parent_id)->first();
            $discount_good = $discount_goods->where('common_id', $recommendGood->common_id)->first();

            $recommendGood->district_name = $district->district_name;
            $recommendGood->district_parent_id = $district->district_parent_id;
            $recommendGood->district_parent_name = $district_parent->district_name;
            $recommendGood->discount_price = $discount_good->discount_price ?? '';
        });

        return $recommendGoods->toArray();
    }
}
